package com.winit.uCloud.manager;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.util.*;
import com.winit.core.server.RedisServer;
import com.winit.core.server.base.CmdOutputHandler;
import com.winit.core.server.factory.LinuxJumpServerFactory;
import com.winit.deploy.server.WinitJumpServer;
import com.winit.jumpServer.bean.JumpServerInfo;
import com.winit.jumpServer.util.JumpServerListInfo;
import com.winit.uCloud.bean.UCloudCInfo;
import com.winit.uCloud.bean.UCloudCRedisInfo;
import com.winit.uCloud.bean.UCloudCTargetInfo;
import com.winit.uCloud.util.UCloudUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangjian on 16/10/28.
 */
@Component
public class UCloudCManager {

    private static Logger logger = LoggerFactory.getLogger(UCloudCManager.class);

    public JSONObject getTotal(String url, String ip) throws Exception {
        //返回值
        JSONObject json = new JSONObject();
        //查询成功后 返回的数据
        JSONObject data = new JSONObject();

        JSONObject j = HttpClientUtil.doGetForJson(url);
        if (!"0".equals(j.getString("code")))
            return j;

        if ("0".equals(j.getString("message"))) {
            json.put("code", "0");
            json.put("message", "ogg同步服务已经停止");
            return json;
        }

        String message = j.getString("message");
        data.put("from", JSONObject.parseObject(message.replace("},{", ",")));
        //远程调用目标端机器获取数据
        UCloudCInfo uCloudCInfo = UCloudUtil.getCTargetInfo(ip);
        if (uCloudCInfo == null) {
            json.put("code", "1");
            json.put("message", "获取c环境配置信息" + ip + "为null.");
            return json;
        }
        UCloudCTargetInfo info = uCloudCInfo.getTarget().get(0);
        Map<String, Object> map = UCloudUtil.getOggTargetTotal(info);
        logger.info("com.winit.uCloud.manager.UCloudCManager.getTotal ip:" + info.getIp() + " ;map:" + map);

        if ((Integer) map.get("code") != 0) {
            logger.error("com.winit.uCloud.manager.UCloudCManager.getTotal 获取" + info.getIp() + "数据量异常." + map.get("message"));
            json.put("code", "1");
            json.put("message", "获取" + info.getIp() + "数据量异常." + map.get("message"));
            return json;
        }

        data.put("to", JSONObject.parseObject(((String) map.get("message")).replace("},{", ",")));
        json.put("code", "0");
        json.put("data", data);
        return json;
    }

    public Map runOggCmd(String ip, String cmd) {
        Map map = new HashMap();
        //远程调用目标端机器获取数据
        UCloudCInfo uCloudCInfo = UCloudUtil.getCTargetInfo(ip);
        if (uCloudCInfo == null) {
            map.put("code", "1");
            map.put("message", "获取c环境配置信息" + ip + "为null.");
            return map;
        }
        UCloudCTargetInfo info = uCloudCInfo.getTarget().get(0);
        map = UCloudUtil.runOggCmd(info, cmd);
        logger.info("com.winit.uCloud.manager.UCloudCManager.runOggCmd ip:" + info.getIp() + ";cmd:" + cmd + " ;map:" + map);
        return map;
    }


    public List<JSONObject> cleanRedis(String ip) throws Exception {
        UCloudCInfo uCloudCInfo = UCloudUtil.getCTargetInfo(ip);
        if (uCloudCInfo == null) {
            throw new Exception("获取c环境配置信息" + ip + "为null.");
        }
        List<UCloudCRedisInfo> list = uCloudCInfo.getRedis();
        if (list.size() == 0) {
            throw new Exception("获取c环境" + ip + "redis列表为null.");
        }

        final List<String> l = new ArrayList<>();
        CmdOutputHandler handler = new CmdOutputHandler() {
            @Override
            public void onMessage(String cmd, int index, String message, Boolean isErrorMessage) {
                l.add(message);
            }
        };

        List<JSONObject> data = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            UCloudCRedisInfo info = list.get(i);
            List<Integer> ports = new ArrayList<>();
            ports.add(6382);
            ports.add(6392);
            RedisUtil.cleanRedis(info.getJumpServer(), info.getIp(), info.getSshPort(), ports, handler);
            JSONObject j = new JSONObject();
            j.put("ip", info.getIp());
            j.put("status", l.get(i));
            data.add(j);
        }
        return data;
    }

    public void getOggLogFile(String ip,String fileName) throws Exception {
        UCloudCInfo uCloudCInfo = UCloudUtil.getCTargetInfo(ip);
        if (uCloudCInfo == null) {
            throw new Exception("获取c环境配置信息" + ip + "为null.");
        }
//        UCloudCTargetInfo info = uCloudCInfo.getTarget().get(0);
        StringBuffer cmd = new StringBuffer();
        cmd.append("sshpass -p ").append(uCloudCInfo.getPassword()).append(" scp -P ").append(uCloudCInfo.getSshPort()).append(" ")
                .append(uCloudCInfo.getUser()).append("@").append(uCloudCInfo.getcIp())
                .append(":/usr/local/OGG/shell/").append(fileName).append(" /usr/local/Beetle/temp/");
        Map<String, Object> map = ShellRunUtil.runCmdsMap(cmd.toString());
        logger.info("com.winit.uCloud.manager.UCloudCManager.getOggLogFile ip:" + uCloudCInfo.getcIp() + " ;map:" + map);
    }

}
